////LIB
////### 
function  dec2hex2 ( textString ) {
	var hexequiv = new Array ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F");
	return hexequiv[(textString >> 4) & 0xF] + hexequiv[textString & 0xF];
}
function convertCharStr2UTF8 ( str ) { 
	// Converts a string of characters to UTF-8 byte codes, separated by spaces
	// str: sequence of Unicode characters
	var highsurrogate = 0;
	var suppCP; // decimal code point value for a supp char
	var n = 0;
	var outputString = '';
	for (var i = 0; i < str.length; i++) {
		var cc = str.charCodeAt(i); 
		if (cc < 0 || cc > 0xFFFF) {
			outputString += '!Error in convertCharStr2UTF16: unexpected charCodeAt result, cc=' + cc + '!';
		}
		if (highsurrogate != 0) {  
			if (0xDC00 <= cc && cc <= 0xDFFF) {
				suppCP = 0x10000 + ((highsurrogate - 0xD800) << 10) + (cc - 0xDC00); 
				outputString += ' '+dec2hex2(0xF0 | ((suppCP>>18) & 0x07)) + ' ' + dec2hex2(0x80 | ((suppCP>>12) & 0x3F)) + ' ' + dec2hex2(0x80 | ((suppCP>>6) & 0x3F)) + ' ' + dec2hex2(0x80 | (suppCP & 0x3F));
				highsurrogate = 0;
				continue;
			}
			else {
				outputString += 'Error in convertCharStr2UTF16: low surrogate expected, cc=' + cc + '!';
				highsurrogate = 0;
			}
		}
		if (0xD800 <= cc && cc <= 0xDBFF) { // high surrogate
			highsurrogate = cc;
		}
		else {
			if (cc <= 0x7F) { outputString += ' '+dec2hex2(cc); }
			else if (cc <= 0x7FF) { outputString += ' '+dec2hex2(0xC0 | ((cc>>6) & 0x1F)) + ' ' + dec2hex2(0x80 | (cc & 0x3F)); } 
			else if (cc <= 0xFFFF) { outputString += ' '+dec2hex2(0xE0 | ((cc>>12) & 0x0F)) + ' ' + dec2hex2(0x80 | ((cc>>6) & 0x3F)) + ' ' + dec2hex2(0x80 | (cc & 0x3F)); } 
		}
	}
	return outputString.substring(1);
}


////Make a nake url: remove header, and tail after ? or #
function Nake_Url(url)
{
    remove = ['http://', 'https://', 'www.'];
    for (i = 0; i < remove.length; i++)
    {
        index = url.indexOf(remove[i]);
        if (index != -1)
        {
            url = url.substring(0, index) + url.substring(index + remove[i].length);
        }
    }
    ////Remove parameters in url
    key = '?';
    index = url.indexOf(key);
    if (index != -1)
    {
        url = url.substring(0,index);
    }
    
    key = '#';
    index = url.indexOf(key);
    if (index != -1)
    {
        url = url.substring(0,index);
    }
    
    
    return escape(url);
}    



////Search and get the tail of string after key
function Str_Get_After_Key(str, key)
{
	if (str == null) {
		str = ''; return str;
	}
    var index = str.indexOf(key);
    if (index != -1)
    {
        return str.substring(index + key.length);
    }
    return str;
}

////Search and get the head of string before key
function Str_Get_Before_Key(str,key)
{
	if (str == null) {
		str = ''; return str;
	}
    var index = str.indexOf(key);
    if (index != -1)
    {
        return str.substring(0, index);
    }
    return str;
}

////Remove content between 2 keys, included the keys
function Remove_Str_Between_Keys(str, key1, key2) {
	if (str == null) {
		str = ''; return str;
	}
	/*
    var result = str;
    if (result.indexOf(key1) == -1) return str;
    var str1 = Str_Get_Before_Key(result, key1);
    var str2 = Str_Get_After_Key(result, key1);
    if (str2.indexOf(key2) == -1) return str;
    str2 = Str_Get_After_Key(str2, key2);
    result = str1 + str2;
    return result;
	*/
	var index1 = str.indexOf(key1);
	if (index1 == -1) return str;
	var index2 = str.indexOf(key2, index1);
	if (index2 == -1) return;
	str = str.substring(0, index1) + str.substring(index2 + key2.length);
	return str;
}

////Get content between 2 keys
function Get_Str_Between_Keys(str, key1, key2) {
	if (str == null) {
		str = ''; return str;
	}
    var result = str;
    if (result.indexOf(key1) == -1) return str;
    result = Str_Get_After_Key(result, key1);
    if (result.indexOf(key2) == -1) return str;
    result = Str_Get_Before_Key(result, key2);
    return result;
}

////Get string between tags (example: function(str, 'body');
function Get_Str_Between_Tag(str, tag) {
	if (str == null) {
		str = ''; return str;
	}
    var result = str;
    if (result.indexOf('<'+tag) == -1) {
        return str;
    }
    result = Str_Get_After_Key(result, '<'+tag);
    if (result.indexOf('>') == -1) {
        return str;
    }
    result = Str_Get_After_Key(result, '>');
    if (result.indexOf('</'+tag+'>') == -1) {
        return str;
    }
    result = Str_Get_Before_Key(result, '</'+tag+'>');
    return result;    
}


////Remove a tag wrapping a text, just keep the text
function Remove_Tag_Wrap(str, tagopen, tagclose) {
    var head = Str_Get_Before_Key(str, tagopen);
    var tail = Str_Get_After_Key(str, tagopen);
    var mid = Str_Get_Before_Key(tail, tagclose);
    tail = Str_Get_After_Key(tail, tagclose);
    mid = Str_Get_After_Key(mid, '>');
    str = head + mid + tail;
    return str;
}

////Get host name of an URL
function Get_Host(url) {
    url = Nake_Url(url);
    url = Str_Get_Before_Key(url, '/');
    return url;
}

////Get image name from URL
function Get_Image_Name(url) {
    while (url.indexOf('/') != -1) {
        temp = Str_Get_After_Key(url, '/');
        if (temp != '') {
            url = temp;
        } else {
            break;
        }
    }
   
    return url;
}


////Show process bar
function Pbar_Show() {
	//jQuery('.process-bar-mask').css('display', 'block');
	jQuery('.process-bar-container').css('display', 'block');
}

////Update process bar
function Pbar_Update(str) {
	jQuery('.process-bar-container .info span').text(str);
}

////Hide process bar
function Pbar_Hide() {
	jQuery('.process-bar-mask').css('display', 'none');
	jQuery('.process-bar-container').css('display', 'none');
}

////Scroll to an element
function Scroll_To(element) {
jQuery('html, body').animate({ scrollTop: jQuery(element).offset().top }, 500);
}
    
////    
function setCookie(c_name,value,exdays)
{
    var exdate=new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
    document.cookie=c_name + "=" + c_value;
}

////
function getCookie(c_name)
{
    var i,x,y,ARRcookies=document.cookie.split(";");
    for (i=0;i<ARRcookies.length;i++)
    {
        x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
        y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
        x=x.replace(/^\s+|\s+$/g,"");
        if (x==c_name)
        {
            return unescape(y);
        }
    }
}    


////Replace all
function Replace_All_Str(source, key, by) {
	//alert('source =::' + source + '::key=::'+key+'::by='+by+'::index='+source.indexOf(key)+'::'+source.indexOf('á'));
	/*
    while (source.indexOf(key) != -1) {
        //source = source.replace(key, by);
		index = source.indexOf(key);
		var head = source.substring(0,index);
		var tail = source.substring(index + key.length);
		source = head + by + tail;		
    }
	*/
	/*
	for (var i = 0; i < source.length; ) {
		if (source.charCodeAt(i) == key.charCodeAt(0)) {
			var head = source.substring(0,i);
			var tail = source.substring(i + 1);
			source = head + by + tail;
			i += by.length;
		} else {
			i++;
		}
	}
	*/
	
	for (var i = 0; i < source.length; i++) {
		index = source.indexOf(key);
		if (index == -1) break;
		source = source.substring(0,index) + by + source.substring(index+key.length);
	}
    return source;
}

function UTF8_To_Ascii(str) {
	for (var i = 0; i < str.length;) {
		code = str.charCodeAt(i);
		// `found a special char
		if (code >= 128 || code == 32) {
			utf8 = convertCharStr2UTF8(str[i]);			
			var head = str.substring(0,i);
			var tail = str.substring(i + 1);
			utf8 = '%' + utf8.replace(' ', '%').replace(' ', '%').replace(' ', '%');
			
			str = head + utf8 + tail;
			i += utf8.length;
		} 
		// else if (code == 37) {
			// if (i+2 < str.length) {
				// code1 = str.charCodeAt(i+1);
				// code2 = str.charCodeAt(i+2);
				
				// if (code1 == 50 && code2 == 53) {
					// i += 2;
				// } else {
					// var head = str.substring(0,i);
					// var tail = str.substring(i + 1);
					// utf8 = '%25';
					// str = head + utf8 + tail;
					// i += utf8.length;
				// }
			// } else {
				// var head = str.substring(0,i);
				// var tail = str.substring(i + 1);
				// utf8 = '%25';
				// str = head + utf8 + tail;
				// i += utf8.length;
			// }
			
		// } 
		else {		
			i++;
		}
	}
	return str;
}


////Remove all unneed characters at LEFT of a string
function Trim_Left(str, remove_keys) {
    for (var i = 0; i < str.length; i++) {        
        for (var j = 0; j < remove_keys.length; j++) {
            if (str[0] == remove_keys[j]) {
                str = str.substring(1);
                break;
            }
        }
        if (j == remove_keys.length) {
            break;
        }
    }
    return str;
}

////Remove all unneed characters at RIGHT of a string
function Trim_Right(str, remove_keys) {
    for (var i = str.length - 1; i >= 0; i--) {        
        for (var j = 0; j < remove_keys.length; j++) {
            if (str[str.length - 1] == remove_keys[j]) {
                str = str.substring(0, str.length - 1);
                break;
            }
        }
        if (j == remove_keys.length) {
            break;
        }
    }
    return str;
}

////Remove all unneed characters at RIGHT and LEFT of a string
function Trim(str, remove_keys) {
    str = Trim_Left(str, remove_keys);
    str = Trim_Right(str, remove_keys);
    return str;
}

function supports_html5_storage() {
  if(typeof(Storage)!=="undefined")
  {
  // Yes! localStorage and sessionStorage support!
  // Some code.....
  return true;
  }
else
  {
  // Sorry! No web storage support..
  return false;
  }
}

jQuery.fn.extend({
    insertAtCaret: function(valueToInsertAtCaret){
        return this.each( function(i) {
            if ( document.selection ) {
                this.focus();
                selection = document.selection.createRange();
                selection.text = valueToInsertAtCaret;
                this.focus();
            } else if ( this.selectionStart || this.selectionStart == "0" ) {
                var startPosition = this.selectionStart;
                var endPosition = this.selectionEnd;
                var scrollTop = this.scrollTop;
                this.value = this.value.substring(0, startPosition) + valueToInsertAtCaret + this.value.substring(endPosition, this.value.length);
                this.focus();
                this.selectionStart = startPosition + valueToInsertAtCaret.length;
                this.selectionEnd = startPosition + valueToInsertAtCaret.length;
                this.scrollTop = scrollTop;
            } else {
                this.value += valueToInsertAtCaret;
                this.focus();
            }
        })
    }
});

